const writeWebLog = errorInfo => {
  window?.NimCefWebInstance?.call('WriteWebLog', errorInfo, () => {});
};

interface Response {
  result: boolean;
  msg: string;
  obj: object;
}

const request = function (funcName: string, body = {}) {
  return new Promise((resolve, reject) => {
    try {
      window?.NimCefWebInstance?.call(funcName, body, (success, res: Response) => {
        if (!res.result) {
          writeWebLog(res);
          resolve(res);
        } else {
          resolve(res);
        }
      });
    } catch (e) {
      writeWebLog(e);
      reject(e);
    }
  });
};

export default request;
